import "babel-polyfill";
import Vue from "vue";

// if(window.plus){
//   setTimeout(() => {
//     console.log('--localStorage--拦截')
//   }, 3e3)
//   Object.defineProperty(window, 'localStorage', {
//     writable: true,
//   })
//   window.localStorage = {
//     getItem: function(key){
//       return window.plus.storage.getItem(key)
//     },
//     setItem: function(key, val){
//       window.plus.storage.setItem(key, val)
//     },
//     removeItem: function(key){
//       window.plus.storage.removeItem(key)
//     },
//     clear: function(){
//       window.plus.storage.clear()
//     }
//   }
// }

import App from "./App.vue";
// 样式
import "@/assets/scss/public.scss";
//全局引入vant组件
import Vant, { List, Lazyload, ImagePreview } from 'vant'
import 'vant/lib/index.css'
import 'vant/lib/icon/local.css'
import router from '../src/router'
import Vuex from 'vuex'
import store from '@/store/index'
import { ajax } from './utils/ajax/index'
import './utils/rem'
import VueClipboard from 'vue-clipboard2'
import '@/plus/webIM/config.js' //环信
import {getVoice,setVoice,clearHot} from '@/utils/chat.js'  //语音授权


import {
  viewInit,
  networkState,
  back
} from '@/utils/plus'
import { MessagePush } from './utils/meaasgePush'
import { getuserUid, getToken, getRole } from '@/utils/auth.js'

// 使用 babel6 polyfills
// import "core-js/shim"
import md5 from "js-md5";
import { Dialog } from "vant";

Vue.prototype.$v = '1.1.8'  //版本号
Vue.prototype.$channel = 'Ae86'  //渠道号
const d = document,

  w = window;
let pause = false;

let run = () => {
  Vue.use(List)
  Vue.use(Vant)
  Vue.use(Lazyload)
  Vue.use(VueClipboard)
  Vue.use(Dialog)

  Vue.use(ImagePreview)

  //导入md5
  Vue.prototype.$md5 = txt => md5(`tuxiaozhu${txt}`)

  w.HTTP = ajax;
  w.getuserUid = getuserUid
  w.getToken = getToken
  w.getRole = getRole
  Vue.config.productionTip = false

  router.beforeEach(function(to, from, next){
    Vue.prototype.$referer = from
    window.TESTVUE && window.TESTVUE.$toast.clear()
    next()
  })
  w.TESTVUE = new Vue({
    router,
    Vuex,
    store,
    render: h => h(App),
  }).$mount('#app')
  MessagePush()
  //MessagePush()
  // document.addEventListener("foreground", () => {
  //   MessagePush()
  // }, false)

}

if (process.env.NODE_ENV == "development") {
  run()
}
document.addEventListener('plusready', () => {
	//如果第一次进来，开启语音权限
	let ExistVoice = getVoice();
	if(!ExistVoice){
		console.log('语音授权');
		let voice = plus.audio.getRecorder();
		voice.record({filename:"_doc/audio/tuxiaozhu/"}, (res)=> {
			console.log(res);
		}, e=>{
			console.log(e);
		});
		//结束录音对象
		setTimeout(()=>{
			voice.stop();
			setVoice('voice');
		},5000)
	}
	//推送的点击事件
	plus.push.addEventListener("click", msg=> {
		console.log('点击事件');
		console.log(msg);
		//跳转页面
        if(msg.payload){
            let obj = JSON.parse(msg.payload);
            //群组
            if(obj.type == 'group'){
                clearHot(obj.groupId); //清除红点
                router.push({
                    path:'/performance/chat',
                    query:{
                        id:obj.groupId,
                        orderId:obj.orderId
                    }
                });
            }
            //客服
            else if(obj.type == 'chatroom'){
                this.$store.commit("serverHot", false);  //清除红点
                router.push({
                    path:'/advisory'
                });
            }
        }
	}, false);
	//打开APP时，清空所有推送
	document.addEventListener("visibilitychange", function() {
            console.log('main.js');
			if(!document.hidden){
					plus.push.clear();
			}
            else{
                MessagePush()
            }
	});
  import('vconsole').then(res => {
    let VConsole = res.default
    new VConsole()
  })
  // import('./utils/switchapi').then(res => {
  //   let switchApi = res.default
  //   switchApi()
  // })
  run()
  // 初始化准备
  viewInit(d, w);

  d.addEventListener(
    "pause",
    () => {
      // 应用从前台切换到后台
      pause = true
    },
    false
  );

  d.addEventListener(
    "resume",
    () => {
      // 应用从后台切换到前台
      pause = false
    },
    false
  );

  // 监听网络状态
  d.addEventListener(
    "netchange",
    () => {
      if (!pause) {
        let state = networkState();
        switch (state) {
          case 0:
            w.plus.nativeUI.toast("网络连接中断");
            break
          case 1:
            w.plus.nativeUI.toast("已切换到移动网络");
            break
          case 2:
            w.plus.nativeUI.toast("已切换到WiFi网络");
            break

          default:
            break
        }
      }
    },
    false
  );

  //关闭启动页
  d.addEventListener(
    "splashclosed",
    () => {
      store.commit("setNetWork", networkState());
    },
    false
  );
  w.plus.key.addEventListener(
    "backbutton",
    () => {
      back({
        route: w.TESTVUE.$route,
        //store: w.myVue.$store
        _this:w.TESTVUE
      })
    },
    false
  );
  // 设置系统状态栏样式为浅色前景色样式
  w.plus.navigator.setStatusBarStyle('light');
  // 存在问题 待解决
  w.plus.navigator.setStatusBarBackground('#5E8EFE');
})
